class Solution {
public:
    int reverse(int x) {
        // -2147483648~2147483647
        int y = 0;
        while(x != 0) {
            if(y > 214748364 || (y == 214748364 && x > 7) || y < -214748364 || (y == -214748364 && x < -8 )) return 0;
            y = (x % 10) + 10 * y;
            x /= 10;
        }
        return y;
    }
};
